package com.lei.library_mvp_retrofit.http.product;

import com.lei.library_mvp_retrofit.http.token.TokenInterceptor;
import com.lei.library_mvp_retrofit.http.url.BaseUrl;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @ProjectName : YoHoJava_002
 * @Author : 雷金泷
 * @Time : 2021/1/8 16:07
 * @Description : 文件描述
 */
public class NetWorkManger implements IRetrofitProduct {
    private Retrofit retrofit;

    @Override
    public void create() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .writeTimeout(5, TimeUnit.SECONDS)
                        .readTimeout(5, TimeUnit.SECONDS)
                        .connectTimeout(5, TimeUnit.SECONDS)
                        .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
//                        .addInterceptor(new TokenInterceptor())
                        .build())
                .build();
    }

    @Override
    public Retrofit getRetrofit() {
        if (retrofit==null){
            create();
        }
        return retrofit;
    }
}
